﻿using MediatR;
using PLC.Domain.Models;
using PLC.Domain.Service.IRepository;

namespace PLC.Domain.Events.Handlers;

public class SaveBarcodeDataHandler:INotificationHandler<SaveBarcodeDataModel>
{
    private readonly IBarcodeDataRepository _barcodeDataRepository;


    public SaveBarcodeDataHandler( IBarcodeDataRepository barcodeDataRepository)
    {
        _barcodeDataRepository = barcodeDataRepository?? throw new ArgumentNullException(nameof(barcodeDataRepository));
    }
    /// <summary>
    /// 保存条码数据
    /// </summary>
    /// <param name="notification"></param>
    /// <param name="cancellationToken"></param>
    /// <returns></returns>
    public async Task Handle(SaveBarcodeDataModel notification, CancellationToken cancellationToken)
    {
        if (notification.BarcodeDataList.Count==0)
        {
            return;
        }
        // 批量插入数据库
        await _barcodeDataRepository.CreateAsync(notification.BarcodeDataList);
    }
}